package zhoufu.sz.sanxiao_check.ui;

import android.content.Intent;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ActivityMapBinding;
import zhoufu.sz.sanxiao_check.location.LocationHelper;


/**
 * @author liuhehong
 * @project MaoMing_Jingdu_Help
 * @packageName com.zf.src.maoming.jingduhelp.ui.activity
 * @date 2016/5/18 15:58
 * @des
 */
public class MapActivity extends BaseActivity
        implements OnGetGeoCoderResultListener
{
    ActivityMapBinding _binding;
    public static final String EXTRA_LOCATION = "bdLocation";

    private BaiduMap mBaiduMap;
    private double   mLatitude;
    private double   mLongitude;
    // 搜索模块，也可去掉地图模块独立使用
    private GeoCoder mSearch = null;


    @Override protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        //LocationHelper.getInstance().startLocation(this);
    }

    @Override protected void initAfterOnCreate(ViewDataBinding binding)
    {
        super.initAfterOnCreate(binding);
        _binding = (ActivityMapBinding) binding;
    }

    @Override protected void initInject()
    {

    }

    @Override protected int setIndividualId()
    {
        return R.layout.activity_map;
    }

    private void initView()
    {
        setTitle("地图");
        mSearch = GeoCoder.newInstance();
        mBaiduMap = _binding.mapView.getMap();
        //mBaiduMap.setOnMapLoadedCallback(() -> requestMyLocation());
        _binding.mapView.showZoomControls(false);
        mSearch.setOnGetGeoCodeResultListener(this);
        mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener()
        {
            @Override
            public void onMapStatusChangeStart(MapStatus arg0)
            {
                _binding.mapviewAddress.setText("正在解码地址...");
            }

            @Override
            public void onMapStatusChangeFinish(MapStatus arg0)
            {
                mLatitude = arg0.target.latitude;
                mLongitude = arg0.target.longitude;
                MapStatusUpdate up = MapStatusUpdateFactory
                        .newLatLngZoom(new LatLng(mLatitude, mLongitude), 19);
                mBaiduMap.setMapStatus(up);
                mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(arg0.target));
            }

            @Override
            public void onMapStatusChange(MapStatus arg0)
            {
            }
        });
        showMyLocation();
    }


    private void showMyLocation()
    {
        mBaiduMap.setMyLocationEnabled(true);
        if ( getIntent().getDoubleExtra(KEY_LATITUDE, 0.0) < 1 )
        {
            mLatitude = LocationHelper.getLatitude();
            mLongitude = LocationHelper.getLongitude();
        } else
        {
            mLatitude = getIntent().getDoubleExtra(KEY_LATITUDE, 0.0);
            mLongitude = getIntent().getDoubleExtra(KEY_LONGITUDE, 0.0);
            if ( mLatitude > mLongitude )
            {
                double temp = mLatitude;
                mLatitude = mLongitude;
                mLongitude = temp;
            }
        }

        //MyLocationData locationData = new MyLocationData.Builder().latitude(mLatitude)
        //        .longitude(mLongitude).accuracy(location.getRadius())
        //        .direction(location.getDirection()).build();
        //mBaiduMap.setMyLocationData(locationData);
        LatLng latLng = new LatLng(mLatitude, mLongitude);
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(latLng));
        MapStatusUpdate up = MapStatusUpdateFactory.newLatLngZoom(latLng, 19);
        mBaiduMap.setMapStatus(up);
        mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(latLng));
        _binding.mapviewAddress.setText(LocationHelper.getAddress());
    }

    private void initData()
    {

    }

    @Override protected void onResume()
    {
        super.onResume();
        _binding.mapView.onResume();
    }

    @Override protected void onPause()
    {
        super.onPause();
        _binding.mapView.onPause();
    }

    @Override protected void onDestroy()
    {
        super.onDestroy();
        _binding.mapView.onDestroy();
        //LocationHelper.getInstance().stopLocation();
    }

    @Override public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_mapview, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override public boolean onOptionsItemSelected(MenuItem item)
    {
        if ( item.getItemId() == R.id.menu_update )
        {
            Intent intent = new Intent();
            intent.putExtra(KEY_ADDRESS, _binding.mapviewAddress.getText().toString());
            intent.putExtra(KEY_LATITUDE, mLatitude);
            intent.putExtra(KEY_LONGITUDE, mLongitude);
            setResult(RESULT_OK, intent);
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    public static final String KEY_ADDRESS   = "key_address";
    public static final String KEY_LATITUDE  = "key_Latitude";
    public static final String KEY_LONGITUDE = "key_Longitude";

    @Override public void onGetGeoCodeResult(GeoCodeResult geoCodeResult)
    {

    }

    @Override public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult)
    {
        ReverseGeoCodeResult.AddressComponent com = reverseGeoCodeResult.getAddressDetail();
        if ( com != null )
        {
            StringBuffer sb = new StringBuffer();
            sb.append(com.province);
            sb.append(com.city);
            sb.append(com.district);
            sb.append(com.street);
            sb.append(com.streetNumber);
            _binding.mapviewAddress.setText(sb.toString());
        }
    }
}
